package model;

import java.util.Date;

/**
 * @author Ceslovas Lopan
 */
public class TaskInfo {
	private Process process;
	private Date startTime;
	private Date finishTime;

	public TaskInfo(Process process) {
		this.process = process;
		startTime = null;
		finishTime = null;
	}

	public Process getProcess() {
		return process;
	}

	public void setProcess(Process process) {
		this.process = process;
	}

	public Date getStartTime() {
		return startTime;
	}

	public void setStartTime(Date startTime) {
		this.startTime = startTime;
	}

	public Date getFinishTime() {
		return finishTime;
	}

	public void setFinishTime(Date finishTime) {
		this.finishTime = finishTime;
	}

	/**
	 * Checks if process is in time limits(between minimum and maximum) at the
	 * moment
	 * 
	 * @return false if it is not in limits, otherwise true
	 */
	public boolean isInLimits() {
		long running = this.calculateRunning();
		if (running < process.getMinTime() || running > process.getMaxTime())
			return false;

		return true;
	}

	/**
	 * Calculates how much time process is running
	 * 
	 * @return Time period in milliseconds
	 */
	public long calculateRunning() {
		Date now = new Date();
		long running = now.getTime() - startTime.getTime();

		return running;
	}

	/**
	 * Calculates how many time left until process reaches maximum time
	 * 
	 * @return Time period in milliseconds
	 */
	public double calculateTimeLeft() {
		double dif = startTime.getTime() + process.getMaxTime()
				- System.currentTimeMillis();

		return dif;
	}

	/**
	 * Finishes the process.
	 * 
	 * @return true if finished on time, false otherwise
	 */
	public boolean finishProcess() {
		if (finishTime == null)
			finishTime = new Date(System.currentTimeMillis());
		return ((finishTime.getTime() - startTime.getTime()) <= process
				.getMaxTime())
				&& (finishTime.getTime() - startTime.getTime() >= process
						.getMinTime()) ? true : false;
	}
}
